<?php
class ml_biz_user_atNick
{
    private $_content;
    private $_aN2u;
    private $_format;
    const NICKLINK_FORMAT = '<a usercard="true" uid="{#uid}" class="ml_linka" href="{#url}">{#nick}</a>';
    public function start($content , $format = '')
    {
        $this->_reset();
        $this->_content = $content;
        $this->_format = $format ? $format : self::NICKLINK_FORMAT ;
        
        $aNicks = ml_tool_nick::findAllAtNick($content);
        if(count($aNicks) < 1)
            return $content;
        
        $this->_aN2u = ml_tool_nick::nicks2uids($aNicks);
        
        foreach ($this->_aN2u as $nick => $uid)
        {
            if(ml_tool_isuid::is_mluid($uid))
            {
                $content = str_replace('@'.$nick , '[u:'.$uid.']' , $content);
            }
        }
        
        return $content;
    }
    
    public function over($content)
    {
        if(count($this->_aN2u) < 1)
            return $content;
        
        foreach ($this->_aN2u as $nick => $uid)
        {
            if(ml_tool_isuid::is_mluid($uid))
            {
                $link = $this->_format;
                $link = str_replace('{#url}' , ml_tool_urlMaker::person_index($uid) , $link);
                $link = str_replace('{#uid}' , $uid , $link);
                $link = str_replace('{#nick}' , '@'.Tool_string::un_html($nick) , $link);
                $content = str_replace('[u:'.$uid.']' , $link , $content);
            }
        }
        return $content;
    }
    
    private function _reset()
    {
        $this->_format = '';
        $this->_aN2u = array();
        $this->_content = '';
        return ;
    }
}
